package com.atguigu.tingshu.album.api;

import com.atguigu.tingshu.album.service.TrackInfoService;
import com.atguigu.tingshu.album.service.VodService;
import com.atguigu.tingshu.common.login.GuiGuLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.album.TrackInfo;
import com.atguigu.tingshu.query.album.TrackInfoQuery;
import com.atguigu.tingshu.vo.album.*;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

@Tag(name = "声音管理")
@RestController
@RequestMapping("api/album")
@SuppressWarnings({"all"})
public class TrackInfoApiController {

    @Autowired
    private TrackInfoService trackInfoService;

    @Autowired
    private VodService vodService;


    /**
     * 用于小程序端专辑页面展示分页声音列表，动态根据用户展示声音付费标识
     * 登录不拦截
     * /api/album/trackInfo/findAlbumTrackPage/{albumId}/{page}/{limit}
     * @param albumId
     * @param page
     * @param limit
     * @return
     */
    @GuiGuLogin(required = false)
    @Operation(summary = "用于小程序端专辑页面展示分页声音列表，动态根据用户展示声音付费标识")
    @GetMapping("/trackInfo/findAlbumTrackPage/{albumId}/{page}/{limit}")
    public Result<Page<AlbumTrackListVo>> findAlbumTrackPage(
            @PathVariable Long albumId,
            @PathVariable Long page,
            @PathVariable Long limit
            ) {
        Long userId = AuthContextHolder.getUserId();
        Page<AlbumTrackListVo> pageAlbumTrackListVo = new Page<>(page, limit);
        pageAlbumTrackListVo = trackInfoService.findAlbumTrackPage(albumId, pageAlbumTrackListVo,userId);
        return Result.ok(pageAlbumTrackListVo);
    }


    /**
     * 获取声音统计信息
     * /api/album/trackInfo/getTrackStatVo/{trackId}
     * @param trackId
     * @return
     */
    @GetMapping("/trackInfo/getTrackStatVo/{trackId}")
    public Result<TrackStatVo> getTrackStatVo(@PathVariable Long trackId) {
        TrackStatVo trackStatVo = trackInfoService.getTrackStatVo(trackId);
        return Result.ok(trackStatVo);
    }



    /**
     * /api/album/trackInfo/uploadTrack
     * 音频上传
     */
    @PostMapping("/trackInfo/uploadTrack")
    public Result<Map<String,String>> uploadTrack(@RequestParam("file") MultipartFile file) {
        Map<String,String> uploadUrl = vodService.uploadTrack(file);
        return Result.ok(uploadUrl);
    }

    // /api/album/trackInfo/saveTrackInfo
    @GuiGuLogin
    @PostMapping("/trackInfo/saveTrackInfo")
    public Result saveTrackInfo(@RequestBody TrackInfoVo trackInfoVo){
        Long userId = AuthContextHolder.getUserId();
        trackInfoService.saveTrackInfo(userId,trackInfoVo);
        return Result.ok();
    }

    // /api/album/trackInfo/findUserTrackPage/{page}/{limit}
    @PostMapping("/trackInfo/findUserTrackPage/{page}/{limit}")
    public Result<Page<TrackListVo>> findUserTrackPage(
            @PathVariable Long page,
            @PathVariable Long limit,
            @RequestBody TrackInfoQuery trackInfoQuery) {
        Long userId = AuthContextHolder.getUserId();
        trackInfoQuery.setUserId(userId);
        Page<TrackListVo> pageModel = trackInfoService.findUserTrackPage(page, limit, trackInfoQuery);
        return Result.ok(pageModel);
    }
    // /api/album/trackInfo/getTrackInfo/{id}
    @GetMapping("/trackInfo/getTrackInfo/{id}")
    public Result<TrackInfo> getTrackInfo(@PathVariable Long id) {
        TrackInfo trackInfo = trackInfoService.getById(id);
        return Result.ok(trackInfo);
    }
    // /api/album/trackInfo/updateTrackInfo/{id}
    @GuiGuLogin
    @PutMapping("/trackInfo/updateTrackInfo/{id}")
    public Result updateTrackInfo(@PathVariable Long id, @RequestBody TrackInfoVo trackInfoVo) {
        Long userId = AuthContextHolder.getUserId();
        trackInfoService.saveTrackInfoById(userId,id, trackInfoVo);
        return Result.ok();
    }
    // /api/album/trackInfo/removeTrackInfo/{id}
    @DeleteMapping("/trackInfo/removeTrackInfo/{id}")
    public Result removeTrackInfo(@PathVariable Long id) {
        trackInfoService.removeTrackInfoById(id);
        return Result.ok();
    }
}

